package com.ljfiction.book.Dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.CountDownTimer;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ljfiction.book.MyView.SuperButton;
import com.ljfiction.book.R;
import com.ljfiction.book.activitys.SelectquhaoActivity;
import com.ljfiction.book.http.HttpManager;
import com.ljfiction.book.http.UserManageBean;
import com.ljfiction.book.tools.SettingManager;
import com.ljfiction.book.tools.StringTool;
import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;
import com.lzy.okhttputils.request.BaseRequest;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

import static com.ljfiction.book.tools.ToastUtils.showToast;

public class PhoneBangding {
    private EditText phone;
    private EditText shuruyanzheng;
    private SuperButton giveyanzheng;
    private SuperButton bangdingok;
    private LinearLayout phonebang;
    private TextView quhaotext;
    private LinearLayout selectquhao;
    private Dialog mDialog;
    private Listener listener;
    private CountDownTimer countDownTimer;
    private Context context;
    private NoBackgLodDialog loadingDialog;

    public PhoneBangding(Context context) {
        mDialog = new Dialog(context, R.style.AppTheme2);
        Window window = mDialog.getWindow();
        this.context = context;
        WindowManager.LayoutParams wl = window.getAttributes();
        wl.x = 0;
        wl.y = 0;
        window.setAttributes(wl);
        window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
        window.setGravity(Gravity.CENTER);
        window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        mDialog.setContentView(R.layout.phonebangding);
        mDialog.setCanceledOnTouchOutside(true);
        phone = mDialog.findViewById(R.id.phone);
        shuruyanzheng = mDialog.findViewById(R.id.shuruyanzheng);
        giveyanzheng = mDialog.findViewById(R.id.giveyanzheng);
        bangdingok = mDialog.findViewById(R.id.bangdingok);
        phonebang = mDialog.findViewById(R.id.phonebang);
        quhaotext = mDialog.findViewById(R.id.quhaotext);
        selectquhao = mDialog.findViewById(R.id.selectquhao);
giveyanzheng.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        if (quhaotext.getText().toString().equals("+86") && StringTool.isxuniphone(phone.getText().toString())) {
            showToast("请输入正确手机号");
        } else if (StringTool.isEmpty(phone.getText().toString())) {
            showToast("请输入手机号");
        } else {
            //验证手机号
            chechNickPhone();
        }
    }
});
bangdingok.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
       verification();
    }
});
selectquhao.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        listener.clickquhao();
    }
});
    }

    public void setListener(Listener listener) {
        this.listener = listener;
    }



    public void setQuhao(String textquhao) {
        quhaotext.setText(textquhao);
    }

    public void show() {
        if (mDialog != null) {
            mDialog.show();
        }

    }

    private void chechNickPhone() {

        OkHttpUtils.get(HttpManager.IP + "/person-checkNickPhone/" + phone.getText().toString())

                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        try {
                            JSONObject jsonObject = new JSONObject(s);
                            if ("200".equals(jsonObject.getString("returnCode"))) {

                                getShortMessage();
                            } else if ("500".equals(jsonObject.getString("returnCode"))) {
                                showToast("已注册");
                            } else if ("400".equals(jsonObject.getString("returnCode"))) {
                                listener.giveyanzhengnoLogin();
                                showToast(jsonObject.getString("mes"));
                                mDialog.dismiss();
                            } else {
                                showToast("发送失败，请稍后再试");
                            }

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                });
    }

    //发验证码
    private void getShortMessage() {
        OkHttpUtils.post(HttpManager.IP + "/verification/sys-getShortMessage")

                .params("userMob", StringTool.deletadd(quhaotext.getText().toString()) + phone.getText().toString())
                .params("type", StringTool.huoqugettypebangding(quhaotext.getText().toString(), "bangDing"))
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        try {
                            JSONObject jsonObject = new JSONObject(s);
                            if ("200".equals(jsonObject.getString("returnCode"))) {
                                showToast("发送成功");
                                countDownTimer = new CountDownTimer(60000, 1000) {
                                    @Override
                                    public void onTick(long millisUntilFinished) {
                                        giveyanzheng.setText("剩" + millisUntilFinished / 1000 + "秒");
                                        giveyanzheng.setClickable(false);
                                        giveyanzheng.setsSolidColor("#D8D8D8");
                                    }

                                    @Override
                                    public void onFinish() {
                                        giveyanzheng.setText("重新发送");
                                        giveyanzheng.setClickable(true);
                                        giveyanzheng.setsSolidColor("#f77583");
                                    }
                                };
                                countDownTimer.start();
                            } else {
                                showToast(jsonObject.getString("msg"));
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }
    private void verification() {
        OkHttpUtils.post(HttpManager.IP + "/verification/person-checkedCode")

                .params("checkCode", shuruyanzheng.getText().toString())
                .params("phoneId",StringTool.deletadd(quhaotext.getText().toString())+ phone.getText().toString())
                .execute(new StringCallback() {
                    @Override
                    public void onBefore(BaseRequest request) {
                        super.onBefore(request);
                        if (loadingDialog == null) {
                            loadingDialog = new NoBackgLodDialog(context, true);
                        }
                        loadingDialog.show();
                    }

                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        if (loadingDialog != null) {
                            loadingDialog.dismis();
                        }
                        try {
                            JSONObject jsonObject = new JSONObject(s);
                            if ("200".equals(jsonObject.getString("returnCode"))) {
                                bangdingphone();
                            } else if ("500".equals(jsonObject.getString("returnCode"))) {
                                showToast(jsonObject.getString("msg"));
                            } else {
                                showToast(jsonObject.getString("msg"));
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });

    }
    private void bangdingphone(){
        OkHttpUtils.post(HttpManager.IP + "/person-updateInfo")

                .params("userPhone",phone.getText().toString())
                .execute(new StringCallback() {
                    @Override
                    public void onBefore(BaseRequest request) {
                        if (loadingDialog==null){
                            loadingDialog=new NoBackgLodDialog(context,true);
                        }
                        loadingDialog.show();
                        super.onBefore(request);
                    }

                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        super.onError(call, response, e);
                        if (loadingDialog != null) {
                            loadingDialog.dismis();
                        }
                    }

                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        if (loadingDialog != null) {
                            loadingDialog.dismis();
                        }
                        try {
                            JSONObject  jsonObject = new JSONObject(s);
                            if ("200".equals(jsonObject.getString("returnCode"))){
                                UserManageBean.userBean.setUserPhone(phone.getText().toString());
                                listener.setok();
                                mDialog.dismiss();
                            }
                            showToast(jsonObject.getString("msg"));

                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                    }
                });
    }
    public interface Listener {
        public void giveyanzhengnoLogin();

        public void setok();

        public void clickquhao();
    }
}
